package de.uni.kassel.campusappver3.view.subview;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
import de.uni.kassel.campusappver3.MainActivity;
import de.uni.kassel.campusappver3.R;
import de.uni.kassel.campusappver3.crawler.ICrawl;
import de.uni.kassel.campusappver3.crawler.MensaCrawler;
import de.uni.kassel.campusappver3.model.CrawlerReturnValue;
import de.uni.kassel.campusappver3.model.Resources;

public class MensaActivity extends Activity implements TabHost.TabContentFactory, ICrawl {
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mensa);
        
        //Check if already information had been crawled
        if(Resources.getInstance().getMensaMeals() == null){
        	MensaCrawler mc = new MensaCrawler(this);
        	//if no internet connection, show a warning
        	if (!MainActivity.internetConnection) {
        		createUI();
        		Toast.makeText(getApplicationContext(), "Keine Internetverbindung.", Toast.LENGTH_SHORT).show();
        		ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        		progressBar.setVisibility(View.INVISIBLE);
        	//else get the information
        	} else {
        		mc.fetch();
        	}
        //else just show the already crawled information
        } else {
        	createUI();
        }
    }

	/**
	 * Created the UI (a tab for each day, select the current day
	 * setting the tab colors, etc)
	 */
	private void createUI() {
		ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
		progressBar.setVisibility(View.INVISIBLE);

		final TabHost host = (TabHost) findViewById(R.id.tabhostmensa); 
		host.setup();
        
        TabSpec monday = host.newTabSpec("Mo");
        monday.setIndicator("MO");
        monday.setContent(this);
        
        TabSpec tuesday = host.newTabSpec("Di");
        tuesday.setIndicator("DI");
        tuesday.setContent(this);
        
        TabSpec wednesday = host.newTabSpec("Mi");
        wednesday.setIndicator("MI");
        wednesday.setContent(this);
        
        TabSpec thursday = host.newTabSpec("Do");
        thursday.setIndicator("DO");
        thursday.setContent(this);
        
        TabSpec friday = host.newTabSpec("Fr");
        friday.setIndicator("FR");
        friday.setContent(this);
        
        host.addTab(monday);
        host.addTab(tuesday);
        host.addTab(wednesday);
        host.addTab(thursday);
        host.addTab(friday);
        
        Calendar c = Calendar.getInstance(); 
        int today = c.get(Calendar.DAY_OF_WEEK) - 2;
        
        today = (today == 5 || today == -1) ? 0 : today;
        host.setCurrentTab(c.get(Calendar.DAY_OF_WEEK) - 2);
		
        Resources.setTabColor(host);
        host.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				Resources.setTabColor(host);
			}
		});

	}

	/**
	 * Each day has a list, which is filled by the meals
	 * fetched from the internet
	 */
	@Override
	public View createTabContent(String tag) {
		
		ListView list = null;
		
		if (tag.equals("Mo")) {
			list = (ListView) findViewById(R.id.listViewMo);
		} else if (tag.equals("Di")) {
			list = (ListView) findViewById(R.id.listViewDi);
		} else if (tag.equals("Mi")) {
			list = (ListView) findViewById(R.id.listViewMi);
		} else if (tag.equals("Do")) {
			list = (ListView) findViewById(R.id.listViewDo);
		} else if (tag.equals("Fr")) {
			list = (ListView) findViewById(R.id.listViewFr);
		} 
		
		//return empty content
		if (!MainActivity.internetConnection) {
			return list;
		}
		
		ArrayList<String> value = Resources.getInstance().getMensaMeals().getValue(tag);
		ArrayAdapter<String> adapter;
		ArrayList<String> listItems = new ArrayList<String>();
		String name = "";
		String price = "";
		String gerichte = "";
		
		for (int i = 0; i < value.size()-1; i += 2) {
			if (value.get(i).length() < 10) continue;
			name = value.get(i).substring(1);
			price = value.get(i + 1);
			gerichte = name + " \n \nPreis: " + price;			
			listItems.add(gerichte);
		}
		listItems.add(" ");

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
	    list.setAdapter(adapter);
	    
        return list;
	}

	@Override
	public void onCrawlFinish(CrawlerReturnValue crv) {
		Resources.getInstance().setMensaMeals(crv);		
		createUI();	
	}

}
